package com.sca.common.excel.module.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.junit.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author: denglei
 * @Date: 2022/4/15 15:16
 * @Description:
 */
@Slf4j
public  class  ExcelListener<T> extends AnalysisEventListener<T> {


    /**
     * 解析的数据
     */
    List<T> list = new ArrayList<>();
    /**
     * 正文起始行
     */
    private Integer headRowNumber;
    /**
     * 合并单元格
     */
    private List<CellExtra> extraMergeInfoList = new ArrayList<>();
    public ExcelListener(Integer headRowNumber) {
        this.headRowNumber = headRowNumber;
    }


    public ExcelListener() {
    }

    @Override
    public void invoke(T data, AnalysisContext context) {
        log.info("解析到数据:{}", JSON.toJSONString(data));
        //System.out.println("行："+context.readRowHolder().getRowIndex());
       // System.out.println("对象："+obj);
       // System.out.println("Map结构："+context.readRowHolder().getCellMap());

        list.add(data);
        //index 从1开始
        int index = context.readRowHolder().getRowIndex();
        // 获取该行的map数据
        Map<Integer, Cell> map = context.readRowHolder().getCellMap();

    }

    /**
     * 加上存储数据库
     */
    public List<T> getData() {
        return list;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }



//    @Override
//    public void  invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context){
//         log.info("解析到一条头数据111:{}", JSON.toJSONString(headMap));
//    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        log.info("读取到了一条额外信息:{}", JSON.toJSONString(extra));
        switch (extra.getType()) {
            case COMMENT: {
                log.info("额外信息是批注,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(), extra.getColumnIndex(),
                        extra.getText());
                break;
            }
            case HYPERLINK: {
                if ("Sheet1!A1".equals(extra.getText())) {
                    log.info("额外信息是超链接,在rowIndex:{},columnIndex;{},内容是:{}", extra.getRowIndex(),
                            extra.getColumnIndex(), extra.getText());
                } else if ("Sheet2!A1".equals(extra.getText())) {
                    log.info(
                            "额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{},"
                                    + "内容是:{}",
                            extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(),
                            extra.getLastColumnIndex(), extra.getText());
                } else {
                    Assert.fail("Unknown hyperlink!");
                }
                break;
            }
            case MERGE: {
                log.info(
                        "额外信息是合并单元格,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}",
                        extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(),
                        extra.getLastColumnIndex());
                if (extra.getRowIndex() >= headRowNumber) {
                    extraMergeInfoList.add(extra);
                }
                break;
            }
            default: {
            }
        }
    }
    public List<CellExtra> getExtraMergeInfoList() {
        return extraMergeInfoList;
    }


}
